<template>
  <div>
        <div @click='pay'>点击购买</div>
  </div>
</template>
<script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
  import axios from 'axios'
  
  export default {
    name: "pay",
    data(){
      return{
            id:11,
            openid:'ojHw-5ZjfZhfAVCGfHjO5VzL4qc4',
            root_url:"http://zhibo.qikeya.com/"        
      }
    },
    mounted(){

      console.log('233');
      
    },
    methods:{
      
      pay(){

          var data={
            id:this.id,
            debug:'IAmAColdBloodedPassword'
          }

            //调取班级支付接口
           axios.post(this.root_url+'Api_v1/order/payClass', data).then((res)=>{
             
             if(res.data.errcode == 0){

                var res_data=res.data.data;
                this.payOrder(res_data.order_sn,res_data.log_id);
             }else{
                console.log('班级支付接口失败');
             }
            
           }).catch(function (error) {


            });

      },
      //发起订单支付
      payOrder(order_sn,log_id){
          var data={
            body:'测试订单',
            detail:'测试',
            openid:this.openid,
            total_fee:'1',
            out_trade_no:order_sn,
            attach:log_id
          }

           axios.get('http://zhaoshang.qikeya.com/wechat/?m=order',{params: data}).then((res)=>{

              if(res.data.errcode  == 0){
                  this.callpay(res.data.data);
              }else{
                  console.log('发起订单支付接口失败');
              }
              

           }).catch(function (error) {


            });
      },
    callpay(data){
          console.log(data);
          if (typeof WeixinJSBridge == "undefined"){
             console.log("undefined");

              if( document.addEventListener ){
                  document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
              }else if (document.attachEvent){
                  document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                  document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
              }
          }else{
             console.log("122233");
              jsApiCall(data);
          }
    },
    jsApiCall(data){
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    // window.location.href='phone.html';
                     alert('支付成功');
                }else{
                    alert('支付失败');
                }
            }
        );
    }




    }
  }
</script>

<style scoped>

</style>
